package cn.edu.tsinghua.hpc.gsmtest.service;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.net.LocalServerSocket;
import android.net.LocalSocket;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import cn.edu.tsinghua.hpc.gsmtest.exceptions.InvalidPduLengthException;
import cn.edu.tsinghua.hpc.gsmtest.utils.RILUtils;

public class MessageReportHandlerService extends Service {
	private static final String TAG = "MessageReportHandlerService";
	private static final String SOCKET_NAME = "status-report";
	private LocalServerSocket mServer;
	private LocalSocket socket;
	private Handler handler;

	public static final int NOT_RECEIVED = 0;
	public static final int RECEIVED = 1;

	private int status = NOT_RECEIVED;
	private String waitAddress = null;

	private ServiceBinder serviceBinder = new ServiceBinder();

	public class ServiceBinder extends Binder {
		public MessageReportHandlerService getService() {
			return MessageReportHandlerService.this;

		}
	}

	@Override
	public IBinder onBind(Intent arg0) {
		Log.d(TAG, "MessageReportHandlerService onBind");
		return serviceBinder;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		new Timer().schedule(new TimerTask() {

			@Override
			public void run() {
				try {
					mServer = new LocalServerSocket(SOCKET_NAME);
					while ((socket = mServer.accept()) != null) {
						InputStream is = socket.getInputStream();
						byte[] sizeArray = new byte[4];

						Arrays.fill(sizeArray, (byte) 0);

						if (is != null) {
							is.read(sizeArray, 0, 4);
							int size = sizeArray[0];
							if (size < 0 || size > 1024) {

							} else {
								byte[] pdu = new byte[size];
								Arrays.fill(pdu, (byte) 0);
								is.read(pdu, 0, size);
								String address = RILUtils
										.parseAddressFromStatusReport(new String(
												pdu).toCharArray());
								Log.d(TAG, "address is " + address);
								if (address.equals(waitAddress)
										&& handler != null) {
									handler.sendEmptyMessage(0);
								}
							}
						}
					}
				} catch (IOException e) {
					Log.d(TAG, e.getMessage());
				} catch (InvalidPduLengthException e) {
					Log.d(TAG, "InvalidPduLengthException");
				}

			}
		}, 0);
	}

	@Override
	public void onStart(Intent intent, int startId) {
		Log.d(TAG, "MessageReportHandlerService started");
	}

	@Override
	public void onDestroy() {
		try {
			if (socket != null) {
				socket.close();
				Log.d(TAG, "socket close");
			}
			if (mServer != null) {
				mServer.close();
				Log.d(TAG, "server close");
			}
		} catch (IOException e) {
			Log.d(TAG, e.getMessage());
		}
	}

	public int getReportStatus() {
		int currentStatus = status;
		this.status = NOT_RECEIVED;
		return currentStatus;
	}

	public void setWaitMessageAddress(String address, Handler handler) {
		waitAddress = address;
		this.handler = handler;
	}
}
